import Storage from 'web-storage-cache'
const localStorage = new Storage()
export function setLocalStorage(key, value) {
  return localStorage.set(key, value)
}
export function getLocalStorage(key) {
  return localStorage.get(key)
}
export function removeLocalStorage(key) {
  return localStorage.delete(key)
}
export function clearLocalStorage(key) {
  return localStorage.clear()
}
// 电子书的存储不一样 所以设置也不一样 为每一本电子书都开一个localstorage空间
export function setBookObject(fileName, key, value) {
  let book = getLocalStorage(`${fileName}-info`)
  if (!book) {
    book = {}
  }
  book[key] = value
  //   存储时为json 获取时为对象
  setLocalStorage(`${fileName}-info`, book)
}

// 获取book对象
export function getBookObject(fileName, key) {
  const book = getLocalStorage(`${fileName}-info`)
  if (book) {
    return book[key]
  } else {
    return null
  }
}

// 存储字体
export function getFontFamily(fileName) {
  return getBookObject(fileName, 'fontFamily')
}
export function saveFontFamily(fileName, fontFamily) {
  setBookObject(fileName, 'fontFamily', fontFamily)
}

// 字号设置
export function getFontSize(fileName) {
  return getBookObject(fileName, 'fontSize')
}

export function saveFontSize(fileName, fontSize) {
  setBookObject(fileName, 'fontSize', fontSize)
}
// en cn
export function getLocale() {
  return getLocalStorage('locale')
}

export function saveLocale(locale) {
  return setLocalStorage('locale', locale)
}

// 主题设置
export function saveTheme(fileName, theme) {
  setBookObject(fileName, 'theme', theme)
}

export function getTheme(fileName) {
  return getBookObject(fileName, 'theme')
}

// 文章信息
export function getLocation(fileName) {
  return getBookObject(fileName, 'location')
}

export function saveLocation(fileName, location) {
  setBookObject(fileName, 'location', location)
}

// 阅读时间
export function getReadTime(fileName) {
  return getBookObject(fileName, 'time')
}

export function saveReadTime(fileName, theme) {
  setBookObject(fileName, 'time', theme)
}

// 电子书信息
export function getCover(fileName) {
  return getBookObject(fileName, 'cover')
}

export function saveCover(fileName, cover) {
  setBookObject(fileName, 'cover', cover)
}

export function getBookmark(fileName) {
  return getBookObject(fileName, 'bookmark')
}

export function saveBookmark(fileName, bookmark) {
  setBookObject(fileName, 'bookmark', bookmark)
}

export function saveBookShelf(shelf) {
  return setLocalStorage('shelf', shelf)
}

export function getBookShelf() {
  return getLocalStorage('shelf')
}
